//
//  SXTabBarController.m
//  Weibo1
//
//  自定义 TabBarController
//  初始化 首页 消息 发现 我 4个业务逻辑控制器  -->  给4个业务逻辑控制器包装一个 SXNavController 导航控制器 --> 把4个导航控制器添加为SXTabBarController 4个子控制器

#import "SXTabBarController.h"
#import "SXHomeController.h"        //首页
#import "SXMsgCenterController.h"   //消息
#import "SXDiscoverController.h"    //发现
#import "SXProfileController.h"     //我
#import "SXNavController.h"         //自定义导航控制器
#import "SXTabBar.h"                //自定义TabBar控件

@interface SXTabBarController () <SXTabBarDelegate>

@end


@implementation SXTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addchildViewControllers]; //添加4个子控制器 首页 消息 发现 我
    [self addCustomTabBar];         //添加自定义TabBar
}

//添加4个子控制器 首页 消息 发现 我
- (void)addchildViewControllers {
    
    // 添加子控制器
    SXHomeController *home = [[SXHomeController alloc] init];
    [self addChildVC:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
    
    SXMsgCenterController *message = [[SXMsgCenterController alloc] init];
    [self addChildVC:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
    
    SXDiscoverController *discover = [[SXDiscoverController alloc] init];
    [self addChildVC:discover title:@"发现" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
    
    SXProfileController *profile = [[SXProfileController alloc] init];
    [self addChildVC:profile title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
}


//添加1个子控制器到 TabBarController 根控制器
- (void)addChildVC:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    childVC.view.backgroundColor = SXRandomColor;
    
    childVC.title = title;
    
    childVC.tabBarItem.image = [UIImage imageNamed:imageName];
    
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    if (iOS7) {
        selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];  //iOS7 中选中图片不使用渲染模式
    }
    childVC.tabBarItem.selectedImage = selectedImage;
    
    // 文字样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = SXColor(123, 123, 123);
    
    // 选中文字样式
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    // 设置文字样式
    [childVC.tabBarItem setTitleTextAttributes:textAttrs           forState:UIControlStateNormal];
    [childVC.tabBarItem setTitleTextAttributes:selectedTextAttrs   forState:UIControlStateSelected];
    
    //把 首页 消息 发现 我 包装成导航控制器的根控制器
    SXNavController *nav = [[SXNavController alloc] initWithRootViewController:childVC];
    
    [self addChildViewController:nav];  // 给TabBarController 添加 导航控制器
}

//添加自定义TabBar控件到TabBarController的tabBar上
- (void)addCustomTabBar
{
    SXTabBar *mytabBar = [[SXTabBar alloc] init];
//    mytabBar.mydelegate = self;
    [self setValue:mytabBar forKeyPath:@"tabBar"];
}


#pragma mark - SXTabBarDelegate
- (void)SXTabBarDidClickPlusButton:(SXTabBar *)tabBar
{
    SXLog(@"test");
}

@end
